package com.fcl.oschina.ui.activity;

import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.widget.TextView;

import com.fcl.mylibrary.ImageLoader.ImageLoader;
import com.fcl.mylibrary.reflect.ViewInject;
import com.fcl.mylibrary.ui.adapter.MainTabAdapter;
import com.fcl.mylibrary.util.StringUtil;
import com.fcl.oschina.R;
import com.fcl.oschina.adapter.ImageAdapter;
import com.fcl.oschina.entity.DetailResult;
import com.fcl.oschina.entity.ImageEntity;
import com.fcl.oschina.entity.TweetDetailEntity;
import com.fcl.oschina.model.DetailModel;
import com.fcl.oschina.model.TweetDetailModel;
import com.fcl.oschina.ui.fragment.CommentFragment;
import com.fcl.oschina.ui.fragment.TestFragment;
import com.fcl.oschina.ui.fragment.TweetCommentFragment;
import com.fcl.oschina.ui.fragment.TweetFragment;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class TweetDetailActivity extends BaseBackActivity implements TweetDetailView{

    private int mId;
    private String mType;
    private TweetDetailModel mDetailModel;

    @ViewInject(R.id.user_avtar)
    CircleImageView mUserAvtar;

    @ViewInject(R.id.user_name)
    TextView mUserTv;

    @ViewInject(R.id.content)
    TextView mContentTv;

    @ViewInject(R.id.date)
    TextView mDateTv;

    @ViewInject(R.id.image_recycler)
    RecyclerView mImageRecycler;
    
    @ViewInject(R.id.tab_layout)
    TabLayout mTabLayout;
    
    @ViewInject(R.id.pager)
    ViewPager mPager;

    private List<ImageEntity> mImageDatas = new ArrayList<>();
    private ImageAdapter mImageAdapter;
    private List<String> mTitles = new ArrayList<>();
    private List<Fragment> mFragments = new ArrayList<>();
    private MainTabAdapter mPagerAdapter;
    private TweetDetailEntity mDetail;

    @Override
    protected void init() {
        super.init();
        mToolbar.setTitle(R.string.tweet_detail);
        Intent intent = getIntent();
        mId = intent.getIntExtra("id", 0);
        mType = "tweet";
        mDetailModel = new TweetDetailModel(mType,mId,this);
        mDetailModel.getDetail();

        mImageAdapter = new ImageAdapter(this,R.layout.item_image,mImageDatas);
        mImageAdapter.setShowFooter(false);
        mImageRecycler.setAdapter(mImageAdapter);
        mImageRecycler.setLayoutManager(new GridLayoutManager(this,3));


    }

    private void initFragment() {
        mFragments.clear();
        TweetCommentFragment commentFragment = new TweetCommentFragment();
        Bundle bundle = new Bundle();
        if (mDetail != null) {
            bundle.putInt("id", mDetail.getId());
        }
        commentFragment.setArguments(bundle);
        TestFragment testFragment = new TestFragment();
        mFragments.add(testFragment);
        mFragments.add(commentFragment);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_tweet_detail;
    }

    @Override
    public void setDetail(TweetDetailEntity detail) {
        mDetail = detail;
        mUserTv.setText(detail.getAuthor().getName());
        mContentTv.setText(Html.fromHtml(detail.getContent()));
        mDateTv.setText(StringUtil.formatSomeAgo(detail.getPubDate()));
        ImageLoader.getInstance().loadImage(detail.getAuthor().getPortrait(), mUserAvtar,true);

        mImageDatas = detail.getImages();
        if (mImageDatas != null) {
            mImageAdapter.setDatas(mImageDatas);
        }

        initFragment();
        mTitles.clear();
        mTitles.add("点赞("+detail.getLikeCount()+")");
        mTitles.add("评论("+detail.getCommentCount()+")");
        mPagerAdapter = new MainTabAdapter(getSupportFragmentManager(),this,mFragments,mTitles);
        mPager.setAdapter(mPagerAdapter);
        mTabLayout.setupWithViewPager(mPager);
        mTabLayout.setTabTextColors(getResources().getColor(R.color.colorSecondaryText),
                getResources().getColor(R.color.colorPrimary));
        mTabLayout.setSelectedTabIndicatorHeight(0);
    }
}
